package view.indice;
import extended.HMS;
import generated.Indice;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Component;
import javax.swing.Box;


/**
 * @author C-Net
 * Panel d'affichage des indices
 */
public class JPIndiceAffichage extends javax.swing.JPanel implements ActionListener  {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4287189211015822441L;
	private JTable IndiceListe;
	private JTable Liste_Indice;

	private JPIndice pere;
	private JLabel lblTitreAff;
	private JLabel lblNom;
	private JTextField txtNom;
	private JLabel lblDescription;
	private JTextArea txtADescription;
	private JTextField txtFileBrowser;
	private JButton btnBrowser;
	private JCheckBox cbxIndice_Lock;
	private JLabel lblHeure_deblocage;
	private JLabel lblAffHeureDebloc;
	private JLabel lblHeure_valide;
	private JLabel lblAffHeure_valide;
	private JLabel lblNiveau;
	private JTextField txtNiveau;
	private JButton btnRetour;
	
	private Component horizontalStrut;
	private Component horizontalStrut_1;
	private Component verticalStrut;
	private Component verticalStrut_1;
	private Component verticalStrut_2;
	private Component verticalStrut_3;
	private Component verticalStrut_4;
	private Component verticalStrut_5;
	private Component verticalStrut_6;
	private Component verticalStrut_7;
	private String strPathFichierIndice;


	public JPIndiceAffichage(JPIndice jpIndice) {
		super();
		this.pere = jpIndice ;
		initGUI();
	}
	
	private void initGUI() {
		try {
			
			GridBagLayout gridBagLayout = new GridBagLayout();
			gridBagLayout.columnWidths = new int[]{20, 31, 4, 66, 24, 25, 45, 20, 0};
			gridBagLayout.rowHeights = new int[]{0, 32, 23, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
			gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
			gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
			setLayout(gridBagLayout);
				
				verticalStrut_7 = Box.createVerticalStrut(20);
				GridBagConstraints gbc_verticalStrut_7 = new GridBagConstraints();
				gbc_verticalStrut_7.insets = new Insets(0, 0, 5, 5);
				gbc_verticalStrut_7.gridx = 1;
				gbc_verticalStrut_7.gridy = 0;
				add(verticalStrut_7, gbc_verticalStrut_7);
			
				lblTitreAff = new JLabel();
				lblTitreAff.setText("Indice");
				lblTitreAff.setFont(new Font("Serif", Font.PLAIN, 24));
				GridBagConstraints gbc_lblTitreAff = new GridBagConstraints();
				gbc_lblTitreAff.gridwidth = 8;
				gbc_lblTitreAff.insets = new Insets(0, 0, 5, 0);
				gbc_lblTitreAff.gridx = 0;
				gbc_lblTitreAff.gridy = 1;
				this.add(lblTitreAff, gbc_lblTitreAff);
			
			btnRetour = new JButton();
			btnRetour.setText("Retour Liste");
			btnRetour.addActionListener(this);
			
			horizontalStrut = Box.createHorizontalStrut(20);
			GridBagConstraints gbc_horizontalStrut = new GridBagConstraints();
			gbc_horizontalStrut.insets = new Insets(0, 0, 5, 5);
			gbc_horizontalStrut.gridx = 0;
			gbc_horizontalStrut.gridy = 2;
			add(horizontalStrut, gbc_horizontalStrut);
			
			verticalStrut = Box.createVerticalStrut(20);
			GridBagConstraints gbc_verticalStrut = new GridBagConstraints();
			gbc_verticalStrut.insets = new Insets(0, 0, 5, 5);
			gbc_verticalStrut.gridx = 1;
			gbc_verticalStrut.gridy = 2;
			add(verticalStrut, gbc_verticalStrut);
			
			horizontalStrut_1 = Box.createHorizontalStrut(20);
			GridBagConstraints gbc_horizontalStrut_1 = new GridBagConstraints();
			gbc_horizontalStrut_1.insets = new Insets(0, 0, 5, 0);
			gbc_horizontalStrut_1.gridx = 7;
			gbc_horizontalStrut_1.gridy = 2;
			add(horizontalStrut_1, gbc_horizontalStrut_1);
			
			lblNom = new JLabel();
			lblNom.setText("Nom  :");
			GridBagConstraints gbc_lblNom = new GridBagConstraints();
			gbc_lblNom.anchor = GridBagConstraints.WEST;
			gbc_lblNom.insets = new Insets(0, 0, 5, 5);
			gbc_lblNom.gridx = 1;
			gbc_lblNom.gridy = 3;
			this.add(lblNom, gbc_lblNom);
			
			txtNom = new JTextField();
			txtNom.setText("nom de l'indice");
			GridBagConstraints gbc_txtNom = new GridBagConstraints();
			gbc_txtNom.fill = GridBagConstraints.BOTH;
			gbc_txtNom.insets = new Insets(0, 0, 5, 5);
			gbc_txtNom.gridwidth = 2;
			gbc_txtNom.gridx = 2;
			gbc_txtNom.gridy = 3;
			this.add(txtNom, gbc_txtNom);
			
			verticalStrut_1 = Box.createVerticalStrut(20);
			GridBagConstraints gbc_verticalStrut_1 = new GridBagConstraints();
			gbc_verticalStrut_1.insets = new Insets(0, 0, 5, 5);
			gbc_verticalStrut_1.gridx = 1;
			gbc_verticalStrut_1.gridy = 4;
			add(verticalStrut_1, gbc_verticalStrut_1);
			
			lblNiveau = new JLabel();
			lblNiveau.setText("Niveau de l'indice :");
			GridBagConstraints gbc_lblNiveau = new GridBagConstraints();
			gbc_lblNiveau.anchor = GridBagConstraints.WEST;
			gbc_lblNiveau.insets = new Insets(0, 0, 5, 5);
			gbc_lblNiveau.gridx = 1;
			gbc_lblNiveau.gridy = 5;
			this.add(lblNiveau, gbc_lblNiveau);
			
			
			txtNiveau = new JTextField();
			txtNiveau.setText("niveau de l'indice");
			GridBagConstraints gbc_txtNiveau = new GridBagConstraints();
			gbc_txtNiveau.fill = GridBagConstraints.BOTH;
			gbc_txtNiveau.insets = new Insets(0, 0, 5, 5);
			gbc_txtNiveau.gridx = 2;
			gbc_txtNiveau.gridy = 5;
			this.add(txtNiveau, gbc_txtNiveau);
			
			verticalStrut_2 = Box.createVerticalStrut(20);
			GridBagConstraints gbc_verticalStrut_2 = new GridBagConstraints();
			gbc_verticalStrut_2.insets = new Insets(0, 0, 5, 5);
			gbc_verticalStrut_2.gridx = 1;
			gbc_verticalStrut_2.gridy = 6;
			add(verticalStrut_2, gbc_verticalStrut_2);
			
			
			lblDescription = new JLabel();
			lblDescription.setText("Description : ");
			GridBagConstraints gbc_lblDescription = new GridBagConstraints();
			gbc_lblDescription.anchor = GridBagConstraints.WEST;
			gbc_lblDescription.insets = new Insets(0, 0, 5, 5);
			gbc_lblDescription.gridx = 1;
			gbc_lblDescription.gridy = 7;
			this.add(lblDescription, gbc_lblDescription);
			
			
			txtADescription = new JTextArea();
			txtADescription.setText(" Description de l'indice ouvert");
			GridBagConstraints gbc_txtADescription = new GridBagConstraints();
			gbc_txtADescription.fill = GridBagConstraints.BOTH;
			gbc_txtADescription.insets = new Insets(0, 0, 5, 5);
			gbc_txtADescription.gridwidth = 5;
			gbc_txtADescription.gridx = 2;
			gbc_txtADescription.gridy = 7;
			this.add(txtADescription, gbc_txtADescription);
			
			verticalStrut_3 = Box.createVerticalStrut(20);
			GridBagConstraints gbc_verticalStrut_3 = new GridBagConstraints();
			gbc_verticalStrut_3.insets = new Insets(0, 0, 5, 5);
			gbc_verticalStrut_3.gridx = 1;
			gbc_verticalStrut_3.gridy = 8;
			add(verticalStrut_3, gbc_verticalStrut_3);
			
			
			lblHeure_deblocage = new JLabel();
			lblHeure_deblocage.setText("Heure Deblocage");
			GridBagConstraints gbc_lblHeure_deblocage = new GridBagConstraints();
			gbc_lblHeure_deblocage.anchor = GridBagConstraints.WEST;
			gbc_lblHeure_deblocage.insets = new Insets(0, 0, 5, 5);
			gbc_lblHeure_deblocage.gridx = 1;
			gbc_lblHeure_deblocage.gridy = 9;
			this.add(lblHeure_deblocage, gbc_lblHeure_deblocage);
			
			lblAffHeureDebloc = new JLabel();
			lblAffHeureDebloc.setText("9h30'45\" ");
			GridBagConstraints gbc_lblAffHeureDebloc = new GridBagConstraints();
			gbc_lblAffHeureDebloc.anchor = GridBagConstraints.WEST;
			gbc_lblAffHeureDebloc.insets = new Insets(0, 0, 5, 5);
			gbc_lblAffHeureDebloc.gridx = 2;
			gbc_lblAffHeureDebloc.gridy = 9;
			this.add(lblAffHeureDebloc, gbc_lblAffHeureDebloc);
			lblHeure_valide = new JLabel();
			lblHeure_valide.setText("Co\u00FBt de l'indice :");
			GridBagConstraints gbc_lblHeure_valide = new GridBagConstraints();
			gbc_lblHeure_valide.anchor = GridBagConstraints.WEST;
			gbc_lblHeure_valide.insets = new Insets(0, 0, 5, 5);
			gbc_lblHeure_valide.gridx = 5;
			gbc_lblHeure_valide.gridy = 9;
			this.add(lblHeure_valide, gbc_lblHeure_valide);
			
			
			lblAffHeure_valide = new JLabel();
			lblAffHeure_valide.setText("1h30'45\" ");
			GridBagConstraints gbc_lblAffHeure_valide = new GridBagConstraints();
			gbc_lblAffHeure_valide.anchor = GridBagConstraints.WEST;
			gbc_lblAffHeure_valide.insets = new Insets(0, 0, 5, 5);
			gbc_lblAffHeure_valide.gridx = 6;
			gbc_lblAffHeure_valide.gridy = 9;
			this.add(lblAffHeure_valide, gbc_lblAffHeure_valide);
			
			verticalStrut_4 = Box.createVerticalStrut(20);
			GridBagConstraints gbc_verticalStrut_4 = new GridBagConstraints();
			gbc_verticalStrut_4.insets = new Insets(0, 0, 5, 5);
			gbc_verticalStrut_4.gridx = 1;
			gbc_verticalStrut_4.gridy = 10;
			add(verticalStrut_4, gbc_verticalStrut_4);
			
			btnBrowser = new JButton();
			btnBrowser.setText("afficher fichier");
			GridBagConstraints gbc_btnBrowser = new GridBagConstraints();
			gbc_btnBrowser.anchor = GridBagConstraints.NORTHWEST;
			gbc_btnBrowser.insets = new Insets(0, 0, 5, 5);
			gbc_btnBrowser.gridx = 1;
			gbc_btnBrowser.gridy = 11;
			this.add(btnBrowser, gbc_btnBrowser);
			btnBrowser.addActionListener(this);
			
			txtFileBrowser = new JTextField();
			txtFileBrowser.setText("");
			GridBagConstraints gbc_txtFileBrowser = new GridBagConstraints();
			gbc_txtFileBrowser.fill = GridBagConstraints.HORIZONTAL;
			gbc_txtFileBrowser.insets = new Insets(0, 0, 5, 5);
			gbc_txtFileBrowser.gridx = 2;
			gbc_txtFileBrowser.gridy = 11;
			this.add(txtFileBrowser, gbc_txtFileBrowser);
			
			verticalStrut_5 = Box.createVerticalStrut(20);
			GridBagConstraints gbc_verticalStrut_5 = new GridBagConstraints();
			gbc_verticalStrut_5.insets = new Insets(0, 0, 5, 5);
			gbc_verticalStrut_5.gridx = 1;
			gbc_verticalStrut_5.gridy = 12;
			add(verticalStrut_5, gbc_verticalStrut_5);
			cbxIndice_Lock = new JCheckBox();
			cbxIndice_Lock.setText("Bloquer l'indice");
			GridBagConstraints gbc_cbxIndice_Lock = new GridBagConstraints();
			gbc_cbxIndice_Lock.anchor = GridBagConstraints.NORTHWEST;
			gbc_cbxIndice_Lock.insets = new Insets(0, 0, 5, 5);
			gbc_cbxIndice_Lock.gridx = 1;
			gbc_cbxIndice_Lock.gridy = 13;
			this.add(cbxIndice_Lock, gbc_cbxIndice_Lock);
			
			verticalStrut_6 = Box.createVerticalStrut(20);
			GridBagConstraints gbc_verticalStrut_6 = new GridBagConstraints();
			gbc_verticalStrut_6.insets = new Insets(0, 0, 5, 5);
			gbc_verticalStrut_6.gridx = 1;
			gbc_verticalStrut_6.gridy = 14;
			add(verticalStrut_6, gbc_verticalStrut_6);
			GridBagConstraints gbc_btnRetour = new GridBagConstraints();
			gbc_btnRetour.anchor = GridBagConstraints.NORTH;
			gbc_btnRetour.gridwidth = 8;
			gbc_btnRetour.gridx = 0;
			gbc_btnRetour.gridy = 15;
			this.add(btnRetour, gbc_btnRetour);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnRetour){
			pere.retourListe();
		}
		if (e.getSource() == btnBrowser){
			Afficherfichier(strPathFichierIndice);
	        
		}
		
	}

	private void Afficherfichier(String strPathFichierIndice2) {
		new JFIndiceFichier(strPathFichierIndice2);
		
	}

	/**
	 * @return the indiceListe
	 */
	public JTable getIndiceListe() {
		return IndiceListe;
	}

	/**
	 * @param indiceListe the indiceListe to set
	 */
	public void setIndiceListe(JTable indiceListe) {
		IndiceListe = indiceListe;
	}

	/**
	 * @return the liste_Indice
	 */
	public JTable getListe_Indice() {
		return Liste_Indice;
	}

	/**
	 * @param liste_Indice the liste_Indice to set
	 */
	public void setListe_Indice(JTable liste_Indice) {
		Liste_Indice = liste_Indice;
	}

	public void InitAffichage(Indice indice) {
		int time = indice.getHeureIndice();
		
		txtNom.setText(indice.getNomIndice());
		txtADescription.setText(indice.getDescriptionIndice());
		
		lblAffHeureDebloc.setText((String.valueOf(HMS.getHour(time))+ "h "+ 
									String.valueOf(HMS.getMinute(time))+"min "+
									String.valueOf(HMS.getSecond(time))+"sec") );

		
		time = indice.getTempsIndice();
		lblAffHeure_valide.setText((String.valueOf(HMS.getHour(time))+ "h "+ 
									String.valueOf(HMS.getMinute(time))+"min "+
									String.valueOf(HMS.getSecond(time))+"sec") );
		strPathFichierIndice = indice.getFichierIndice();
		txtFileBrowser.setText(strPathFichierIndice);
		
		txtNiveau.setText(String.valueOf(indice.getNiveauIndice()) );
		cbxIndice_Lock.setSelected(false);
		
		
	}


}
